package cn.itsource.ymcc.config;

import cn.itsource.ymcc.domain.Permission;
import cn.itsource.ymcc.mapper.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private PermissionMapper permissionMapper;

    //配置认证管理器，授权模式为“poassword”时会用到
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }


    private List<String> passPath(){
        List<String> list = new ArrayList<>();
//        list.add("/ymcc/uaa/login/**");
//        list.add("/ymcc/uaa/v2/api-docs");
//        list.add("/ymcc/common/common/**");
//        list.add("/ymcc/user/user/register");
//        list.add("/ymcc/course/courseType/getCourseTypeTree");
//        list.add("/ymcc/course/courseType/loadCrumbs/**");
//        list.add("/ymcc/search/**");
//        list.add("/ymcc/pay/payOrder/notifyUrl");
        list.add("/login/goLogin");
        return list;
    }


    //授权规则配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        List<String> list = passPath();
//        String[] strings = list.toArray(new String[list.size()]);

        //不需要拦截额
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry = http.formLogin()                              //允许表单登录
                .successForwardUrl("/ymcc/uaa/login/gogogo")             // 设置登陆成功页   前后端分离 和微服务都不需用
                .and().authorizeRequests();
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.AuthorizedUrl authorizedUrl = null;
        for (String s : list) {
            authorizedUrl = expressionInterceptUrlRegistry                                //授权配置
                    .antMatchers(s);
        }
        //需要拦截 也需要权限
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry1
                = authorizedUrl.permitAll();

        List<Permission> permissions = permissionMapper.selectList(null);
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry admin = null;
        for (Permission permission : permissions) {
            admin = expressionInterceptUrlRegistry1 //登录路径放行
                    .antMatchers(permission.getResource()).hasAuthority(permission.getSn());
        }
        admin//需要什么权限
                .anyRequest().authenticated()                   //其他路径都要认证之后才能访问
                .and().logout().permitAll()                    //登出路径放行 /logout
                .and().csrf().disable();                        //关闭跨域伪造检查  可以提供效率
    }
}